<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>人员新增</title>
    <script src="../../../../../res/js/jquery-3.4.1.min.js"></script>
    <script charset="utf-8" src="../../../../../res/layui/layui.js"></script>
    <script src="../../../../../res/js/Tools.js"></script>
    <link href="../../../../../res/layui/css/layui.css" media="all" rel="stylesheet">
    <link href="../../../../../res/echarts/eleTree.css" rel="stylesheet"/>
    <script src="../../../../../res/echarts/eleTree.js"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../../res/images/favicon.ico">
    <script charset="utf-8" src="../../../../../res/js/login_common.js"></script>
    <style type="text/css">
        body {
            padding: 10px;
        }
    </style>
</head>
<body>
<form action="" class="layui-form layui-form-pane">
    <input type="hidden" id="pkCompanyId" name="pkCompanyId">
    <div class="layui-form-item">
        <label class="layui-form-label">公司名</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" lay-reqtext="公司名是必填项，岂能为空？" lay-verify="required"
                   name="companyName" id="companyName" placeholder="请输入(必填)" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司地址</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" lay-reqtext="公司地址是必填项，岂能为空？" lay-verify="required"
                   name="companyArea" id="companyArea" placeholder="请输入(必填)" type="text">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属公司</label>
        <div class="layui-input-block">
            <select name="companySuperior" id="companySuperior">
                <option value=""></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">公司负责人</label>
            <div class="layui-input-inline">
                <select name="leaderSuperior" id="leaderSuperior">
                    <option selected="" value="">公司负责人</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label" style="display: inline-block;">上级领导</label>
            <div class="layui-input-inline">
                <select name="companyLeader" id="companyLeader">
                    <option selected="" value="">上级领导</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input autocomplete="off" class="layui-input" name="companyEmil" id="companyEmil"
                       placeholder="请输入" type="text">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">邮编</label>
            <div class="layui-input-inline">
                <input autocomplete="off" class="layui-input" lay-reqtext="" name="companyPostcode"
                       id="companyPostcode" placeholder="请输入" type="text">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea type="textarea" id="remark" name="remark" class="layui-textarea">
            </textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn site-demo-active" id="sub" lay-filter="de1" lay-submit="" type="submit">立即提交</button>
        </div>
    </div>
</form>
<script>
    var form = null;
    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听提交
        form.on('submit(de1)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');

            $.ajax({
                url: getRootPath() + "OaCompanyAction/addOrUpdateCompany"
                , contentType: "application/json;charset=UTF-8"
                , data: JSON.stringify(data.field)
                , headers: {token: token}
                , type: "POST",
                success: res => {
                    if (res.code === 1) {
                        if (res.data.code === 1) {
                            window.parent.location.reload();//刷新父页面
                            const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);
                        } else if (res.data.code === 500) {
                            error('500');
                        } else {
                            layer.msg("新增失败");
                        }
                    } else {
                        layer.msg(404);
                    }
                },
                error: res => {
                    console.log('9999');
                }
            });
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });
        //加载领导下拉框
        loadPerson();
        loadcompany();

        //修改操作
        var ide = getQueryString("pkCompanyId");
        if (ide) {
            findById(ide);//修改页面的赋值
        }

        //重新渲染表单函数
        function renderForm() {
            layui.use('form', function () {
                var form = layui.form(); //高版本建议把括号去掉，有的低版本，需要加()
                form.render();
            });
        }

        form.render();
    });

    function findById(ide) {
        let url=getRootPath() + "/OaCompanyAction/selectById/" + ide;
        let data=getAjaxById(url);
            $("#pkCompanyId").val(data.data.pkCompanyId);
            $("#companyName").val(data.data.companyName);
            $("#companyArea").val(data.data.companyArea);
            $("#companyEmil").val(data.data.companyEmil);
            $("#newsTypeName").val(data.data.newsTypeName);
            $("#companyPostcode").val(data.data.companyPostcode);
            $("#remark").val(data.data.remark);
            $("#companyLeader").val(data.data.companyLeader);
            $("#leaderSuperior").val(data.data.leaderSuperior);
            $("#companySuperior").val(data.data.companySuperior);
            layui.form.render("select");
    }

    function loadPerson() {
        var url = getRootPath() + "OaPersonnelAction/personnelFindByAll";
        var data = getAjax(url, null);
        $.each(data.data, function (index, item) {
            $("#leaderSuperior").append("<option value='" + item.pkPersonnelId + "'>" + item.personnelName + "</option>");
            $("#companyLeader").append("<option value='" + item.pkPersonnelId + "'>" + item.personnelName + "</option>");
        });
    }

    function loadcompany() {
        var url = getRootPath() + "OaCompanyAction/companyFindByAll";
        var data = getAjax(url, null);
        $.each(data.data, function (index, item) {
            $("#companySuperior").append("<option value='" + item.pkCompanyId + "'>" + item.companyName + "</option>");
        });
    }

</script>
</body>
</html>